package platform.standard.video;

import java.awt.image.*;



import engine.Constants;
import engine.Globals;
import engine.video.blit.PixelBuffer_I;

/**
 * AwtScreenBuffer Allows the presentation of video frames to the user by
 * privinding linkage between the java graphics platform and the game engine.
 **/
public final class AwtScreenBuffer implements PixelBuffer_I {
	/**
	 * Constructor
	 * 
	 * @param width
	 *            - width of the screen buffer
	 * @param height
	 *            - height of the screen buffer
	 **/
	public AwtScreenBuffer(int width, int height) {
		_ColorModel = generateColorModel();
		resize(width, height);
	}

	/**
	 * Resizes the screen buffer. This method does not preserve old pixel data.
	 * 
	 * @param width
	 *            - new width of the screen buffer
	 * @param height
	 *            - new height of the screen buffer
	 **/
	public void resize(int width, int height) {
		_Width = width;
		_Height = height;
		_Size = width * height;
		_Image = new BufferedImage(_Width, _Height,
				BufferedImage.TYPE_BYTE_INDEXED, _ColorModel);
		
		_Buffer = (DataBufferByte) _Image.getRaster().getDataBuffer();
	}

	/**
	 * @returns an Image class that can be rendered to a window.
	 **/
	public BufferedImage getImage() {
		return _Image;
	}

	private IndexColorModel generateColorModel() {
		return new IndexColorModel(8, Constants.PALETTE_ENTRIES,
				Globals.GamePalette.getEntries(), 0 , false, -1,
				DataBuffer.TYPE_BYTE);
	}

	private int _Width;
	private int _Height;
	private int _Size; // in pixels
	private IndexColorModel _ColorModel;
	private BufferedImage _Image;
	private DataBufferByte _Buffer;

	// PixelBuffer_I methods --------------------------------------------------

	@Override
	public int getWidth() {
		// TODO Auto-generated method stub
		return _Width;
	}

	@Override
	public int getHeight() {
		// TODO Auto-generated method stub
		return _Height;
	}

	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return _Size;
	}

	@Override
	public byte[] getScan0() {
		// TODO Auto-generated method stub
		return _Buffer.getData();
	}
}